﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Temp
{
	internal class RevitPipeSystemUpdate
	{
		public string ErrorString { get; set; }

		public RevitPipeSystemUpdate(HYPipeSystemDocument doc, Autodesk.Revit.DB.Document rvtDoc)
		{
			this.m_Document = doc;
			this.m_RevitDocument = rvtDoc;
		}

		public bool Update()
		{
			bool flag = true;
			IOrderedEnumerable<HYMEPObject> orderedEnumerable = from strt in this.m_Document.PipeSystem.Objects
			where strt is HYPipeStraight
			orderby strt.Flow
			select strt;
			IEnumerable<HYMEPObject> enumerable = from ft in this.m_Document.PipeSystem.Objects
			where ft is HYPipeFitting
			select ft;
			IEnumerable<HYMEPObject> enumerable2 = from acc in this.m_Document.PipeSystem.Objects
			where acc is HYPipeAccessory
			select acc;
			this.m_RevitPipes = new List<Pipe>();
			bool result;
			using (Transaction transaction = new Transaction(this.m_RevitDocument, "鸿业水力计算结果赋回图面"))
			{
				try
				{
					transaction.Start();
					foreach (HYMEPObject hymepobject in orderedEnumerable)
					{
						HYPipeStraight hypipeStraight = (HYPipeStraight)hymepobject;
						ElementId elementId = new ElementId(hypipeStraight.Id);
						Element elementById = this.m_RevitDocument.GetElementById(elementId);
						if (elementById != null)
						{
							Pipe pipe = elementById as Pipe;
							if (pipe != null)
							{
								this.m_RevitPipes.Add(pipe);
								flag &= RevitPipeSystemUpdate.ToPipe(hypipeStraight, pipe);
							}
						}
					}
					foreach (HYMEPObject hymepobject2 in enumerable)
					{
						HYPipeFitting hypipeFitting = (HYPipeFitting)hymepobject2;
						ElementId elementId2 = new ElementId(hypipeFitting.Id);
						Element elementById2 = this.m_RevitDocument.GetElementById(elementId2);
						if (elementById2 != null)
						{
							FamilyInstance familyInstance = elementById2 as FamilyInstance;
							if (familyInstance != null)
							{
								flag &= RevitPipeSystemUpdate.ToFitting(hypipeFitting, familyInstance);
							}
						}
					}
					foreach (HYMEPObject hymepobject3 in enumerable2)
					{
						HYPipeAccessory hypipeAccessory = (HYPipeAccessory)hymepobject3;
						ElementId elementId3 = new ElementId(hypipeAccessory.Id);
						Element elementById3 = this.m_RevitDocument.GetElementById(elementId3);
						if (elementById3 != null)
						{
							FamilyInstance familyInstance2 = elementById3 as FamilyInstance;
							if (familyInstance2 != null)
							{
								flag &= RevitPipeSystemUpdate.ToAccessory(hypipeAccessory, familyInstance2);
							}
						}
					}
					this.m_RevitDocument.Regenerate();
					transaction.Commit();
					this.ErrorString = "更新操作完成！";
					flag &= this.SaveLinkedUpdateData(orderedEnumerable.ToList<HYMEPObject>(), enumerable2.ToList<HYMEPObject>(), enumerable.ToList<HYMEPObject>());
					result = true;
				}
				catch (Exception ex)
				{
					this.ErrorString = ex.Message;
					if (transaction != null && (int)transaction.GetStatus() != 3)
					{
						transaction.RollBack();
					}
					result = false;
				}
			}
			return result;
		}

		protected bool SaveLinkedUpdateData(List<HYMEPObject> hyStrts, List<HYMEPObject> Accessories, List<HYMEPObject> Fittings)
		{
			bool flag = true;
			List<LinkedDocument> list = RevitUtility.LinkedDocuments.ToList<LinkedDocument>();
			if (list.Count > 0 && YJKMessageBox.Show("当前文档存在链接文件，是否保存链接文件改动数据？", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				string text = this.m_RevitDocument.Title;
				string arg = text;
				if (text.LastIndexOf(".rvt") > 0)
				{
					arg = text.Substring(0, text.LastIndexOf(".rvt"));
				}
				string text2 = string.Format("水管水力计算_InFile_{0}.xml", arg);
				string empty = string.Empty;
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.Filter = "数据更新文件|*.xml";
				if (!string.IsNullOrEmpty(this.lastSelectedPath))
				{
					saveFileDialog.InitialDirectory = this.lastSelectedPath;
				}
				saveFileDialog.RestoreDirectory = true;
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.FileName = text2;
				if (saveFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						text2 = saveFileDialog.FileName;
						this.lastSelectedPath = text2;
						List<string> list2 = new List<string>();
						foreach (LinkedDocument linkedDocument in list)
						{
							text = linkedDocument.Document.Title;
							if (text.LastIndexOf(".rvt") > 0)
							{
								text = text.Substring(0, text.LastIndexOf(".rvt"));
							}
							list2.Add(text);
						}
						flag &= HYUpdateLinkedDocXML.AddRootAndFileListNodes(text2, MajorSystemType.PipeOfHVACSystem, list2);
						foreach (LinkedDocument linkedDocument2 in list)
						{
							text = linkedDocument2.Document.Title;
							if (text.LastIndexOf(".rvt") > 0)
							{
								text = text.Substring(0, text.LastIndexOf(".rvt"));
							}
							List<HYUpdateElementInfo> list3 = new List<HYUpdateElementInfo>();
							this.UpdateLinkedPipes(linkedDocument2.Document, text, list3, hyStrts);
							this.UpdateLinkedAccessories(linkedDocument2.Document, text, list3, Accessories);
							this.UdpateLinkedFittings(linkedDocument2.Document, text, list3, Fittings);
							flag &= HYUpdateLinkedDocXML.AddElementListToXML(text2, text, list3);
						}
					}
					catch (Exception ex)
					{
						ex.ToString();
						flag = false;
						YJKMessageBox.Error("保存链接文件数据失败！");
					}
				}
			}
			return flag;
		}

		protected void UpdateLinkedPipes(Autodesk.Revit.DB.Document doc, string docTitle, List<HYUpdateElementInfo> changedElements, List<HYMEPObject> hyStrts)
		{
			foreach (HYMEPObject hymepobject in hyStrts)
			{
				HYPipeStraight hypipeStraight = (HYPipeStraight)hymepobject;
				Element elementById = doc.GetElementById(new ElementId(hypipeStraight.Id));
				if (elementById != null)
				{
					HYUpdateElementInfo hyupdateElementInfo = new HYUpdateElementInfo();
					hyupdateElementInfo.DocumentTitle = docTitle;
					hyupdateElementInfo.ElementID = hypipeStraight.Id;
					Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeCurves);
					hyupdateElementInfo.CategoryName = category.Name;
					hyupdateElementInfo.PipeDN = hypipeStraight.DN;
					changedElements.Add(hyupdateElementInfo);
				}
			}
		}

		protected void UpdateLinkedAccessories(Autodesk.Revit.DB.Document doc, string docTitle, List<HYUpdateElementInfo> changedElements, List<HYMEPObject> Accessories)
		{
			foreach (HYMEPObject hymepobject in Accessories)
			{
				HYPipeAccessory hypipeAccessory = (HYPipeAccessory)hymepobject;
				Element elementById = doc.GetElementById(new ElementId(hypipeAccessory.Id));
				if (elementById != null)
				{
					HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = new HYUpdateAttachedEleInfo();
					hyupdateAttachedEleInfo.DocumentTitle = docTitle;
					hyupdateAttachedEleInfo.ElementID = hypipeAccessory.Id;
					Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeAccessory);
					hyupdateAttachedEleInfo.CategoryName = category.Name;
					HYPipeStraight hypipeStraight = hypipeAccessory.GetHostPipeline() as HYPipeStraight;
					if (hypipeStraight != null)
					{
						HYPipeSpecification section = hypipeStraight.Section;
						if (section != null)
						{
							hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
							hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
							this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hypipeStraight, section);
						}
					}
					changedElements.Add(hyupdateAttachedEleInfo);
				}
			}
		}

		protected void UdpateLinkedFittings(Autodesk.Revit.DB.Document doc, string docTitle, List<HYUpdateElementInfo> changedElements, List<HYMEPObject> Fittings)
		{
			foreach (HYMEPObject hymepobject in Fittings)
			{
				HYPipeFitting hypipeFitting = (HYPipeFitting)hymepobject;
				Element elementById = doc.GetElementById(new ElementId(hypipeFitting.Id));
				FamilyInstance familyInstance = elementById as FamilyInstance;
				if (elementById != null && familyInstance != null)
				{
					MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
					if (mechanicalFitting != null)
					{
						HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = new HYUpdateAttachedEleInfo();
						hyupdateAttachedEleInfo.DocumentTitle = docTitle;
						hyupdateAttachedEleInfo.ElementID = hypipeFitting.Id;
						Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeFitting);
						hyupdateAttachedEleInfo.CategoryName = category.Name;
                        switch ((int)mechanicalFitting.PartType)
						{
						case 5:
						{
							HYPipeElbow hypipeElbow = hypipeFitting as HYPipeElbow;
							if (hypipeElbow != null)
							{
								HYPipeStraight hypipeStraight = hypipeElbow.GetHostPipeline() as HYPipeStraight;
								if (hypipeStraight != null)
								{
									HYPipeSpecification section = hypipeStraight.Section;
									if (section != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hypipeStraight, section);
									}
								}
							}
							break;
						}
						case 6:
						{
							HYPipeTee hypipeTee = hypipeFitting as HYPipeTee;
							if (hypipeTee != null)
							{
								HYPipeStraight hypipeStraight2 = hypipeTee.GetPreviousPipeline() as HYPipeStraight;
								if (hypipeStraight2 != null)
								{
									HYPipeSpecification section2 = hypipeStraight2.Section;
									if (section2 != null)
									{
										hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.InElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.InElementInfo, hypipeStraight2, section2);
									}
								}
								HYPipeStraight hypipeStraight3 = hypipeTee.GetHostPipeline() as HYPipeStraight;
								if (hypipeStraight3 != null)
								{
									HYPipeSpecification section3 = hypipeStraight3.Section;
									if (section3 != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hypipeStraight3, section3);
									}
								}
								HYPipeStraight hypipeStraight4 = hypipeTee.GetSideHostPipeline() as HYPipeStraight;
								if (hypipeStraight4 != null)
								{
									HYPipeSpecification section4 = hypipeStraight4.Section;
									if (section4 != null)
									{
										hyupdateAttachedEleInfo.SideElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.SideElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.SideElementInfo, hypipeStraight4, section4);
									}
								}
							}
							break;
						}
						case 7:
						{
							HYPipeTransition hypipeTransition = hypipeFitting as HYPipeTransition;
							if (hypipeTransition != null)
							{
								HYPipeStraight hypipeStraight5 = hypipeTransition.GetPreviousPipeline() as HYPipeStraight;
								if (hypipeStraight5 != null)
								{
									HYPipeSpecification section5 = hypipeStraight5.Section;
									if (section5 != null)
									{
										hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.InElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.InElementInfo, hypipeStraight5, section5);
									}
								}
								HYPipeStraight hypipeStraight6 = hypipeTransition.GetHostPipeline() as HYPipeStraight;
								if (hypipeStraight6 != null)
								{
									HYPipeSpecification section6 = hypipeStraight6.Section;
									if (section6 != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hypipeStraight6, section6);
									}
								}
							}
							break;
						}
						case 8:
						{
							HYPipeCross hypipeCross = hypipeFitting as HYPipeCross;
							if (hypipeCross != null)
							{
								HYPipeStraight hypipeStraight7 = hypipeCross.GetPreviousPipeline() as HYPipeStraight;
								if (hypipeStraight7 != null)
								{
									HYPipeSpecification section7 = hypipeStraight7.Section;
									if (section7 != null)
									{
										hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.InElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.InElementInfo, hypipeStraight7, section7);
									}
								}
								HYPipeStraight hypipeStraight8 = hypipeCross.GetHostPipeline() as HYPipeStraight;
								if (hypipeStraight8 != null)
								{
									HYPipeSpecification section8 = hypipeStraight8.Section;
									if (section8 != null)
									{
										hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.OutElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.OutElementInfo, hypipeStraight8, section8);
									}
								}
								HYPipeStraight hypipeStraight9 = hypipeCross.GetSideHostPipeline() as HYPipeStraight;
								if (hypipeStraight9 != null)
								{
									HYPipeSpecification section9 = hypipeStraight9.Section;
									if (section9 != null)
									{
										hyupdateAttachedEleInfo.SideElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.SideElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.SideElementInfo, hypipeStraight9, section9);
									}
								}
								HYPipeStraight hypipeStraight10 = hypipeCross.GetSide2HostPipeline() as HYPipeStraight;
								if (hypipeStraight10 != null)
								{
									HYPipeSpecification section10 = hypipeStraight10.Section;
									if (section10 != null)
									{
										hyupdateAttachedEleInfo.Side2ElementInfo = new HYUpdateElementInfo();
										hyupdateAttachedEleInfo.Side2ElementInfo.DocumentTitle = docTitle;
										this.AssignAttachElementInfo(doc, hyupdateAttachedEleInfo.Side2ElementInfo, hypipeStraight10, section10);
									}
								}
							}
							break;
						}
						}
						changedElements.Add(hyupdateAttachedEleInfo);
					}
				}
			}
		}

		protected bool AssignAttachElementInfo(Autodesk.Revit.DB.Document doc, HYUpdateElementInfo node, HYPipeStraight pipe, HYPipeSpecification ps)
		{
			bool result = false;
			if (node != null && pipe != null && ps != null)
			{
				node.ElementID = pipe.Id;
				node.PipeDN = pipe.DN;
				Element elementById = doc.GetElementById(new ElementId(pipe.Id));
				if (elementById != null)
				{
					Category category = elementById.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeAccessory);
					node.CategoryName = category.Name;
				}
				result = true;
			}
			return result;
		}

		public bool UpdateTransition()
		{
			IEnumerable<HYMEPObject> enumerable = from ft in this.m_Document.PipeSystem.Objects
			where ft is HYPipeFitting
			select ft;
			IEnumerable<HYMEPObject> enumerable2 = from ft in this.m_Document.PipeSystem.Objects
			where ft is HYPipeStraight
			select ft;
			bool result;
			using (Transaction transaction = new Transaction(this.m_RevitDocument, "更新过渡件"))
			{
				try
				{
					transaction.Start();
					foreach (HYMEPObject hymepobject in enumerable)
					{
						ElementId elementId = new ElementId(((HYPipeFitting)hymepobject).Id);
						Element elementById = this.m_RevitDocument.GetElementById(elementId);
						if (elementById != null)
						{
							FamilyInstance familyInstance = elementById as FamilyInstance;
							if (familyInstance != null)
							{
								RevitPipeSystemUpdate.SetTransition(familyInstance);
							}
						}
					}
					foreach (HYMEPObject hymepobject2 in enumerable2)
					{
						ElementId elementId2 = new ElementId(((HYPipeStraight)hymepobject2).Id);
						Element elementById2 = this.m_RevitDocument.GetElementById(elementId2);
						if (elementById2 != null)
						{
							Pipe pipe = elementById2 as Pipe;
							if (pipe != null)
							{
								RevitPipeSystemUpdate.SetTransition(pipe);
							}
						}
					}
					transaction.Commit();
					result = true;
				}
				catch (Exception ex)
				{
					this.ErrorString = ex.Message;
					transaction.RollBack();
					result = false;
				}
			}
			return result;
		}

		private static bool SetTransition(FamilyInstance rvtFi)
		{
			if (!(rvtFi.MEPModel is MechanicalFitting))
			{
				return false;
			}
			new YJKRotateFamilyInstance().MoveFamilyInstance(rvtFi, new XYZ(1.0, 0.0, 0.0) * 0.005);
			rvtFi.Document.Regenerate();
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(rvtFi);
			if (elementAllConnector == null || elementAllConnector.Count <= 0)
			{
				return false;
			}
			foreach (Connector connector in elementAllConnector)
			{
				if (connector != null)
				{
					RevitPipeSystemUpdate.SetReduce(connector);
				}
			}
			return true;
		}

		private static bool SetTransition(Pipe rvtPipe)
		{
			List<Connector> connectorAll = YJKRevitTools.GetConnectorAll(rvtPipe);
			if (connectorAll == null || connectorAll.Count <= 0)
			{
				return false;
			}
			foreach (Connector connector in connectorAll)
			{
				if (connector != null)
				{
					RevitPipeSystemUpdate.SetReduce(connector);
				}
			}
			return true;
		}

		private static bool ToPipe(HYPipeStraight hyStraight, Pipe element)
		{
			RevitUtils.SetDistance(element, BuiltInParameter.RBS_PIPE_DIAMETER_PARAM, (double)hyStraight.DN / 1000.0);
			return true;
		}

		private static void ToConnector(HYPipeSpecification ds, Connector conn)
		{
			if (conn == null)
			{
				return;
			}
			try
			{
				conn.Radius = RevitUtils.DistanceToStd((double)ds.DN / 2000.0);
			}
			catch (Exception)
			{
			}
		}

		private static bool ToAccessory(HYPipeAccessory hyAcc, FamilyInstance rvtFi)
		{
			HYPipeStraight hypipeStraight = hyAcc.GetHostPipeline() as HYPipeStraight;
			if (hypipeStraight == null)
			{
				return false;
			}
			HYPipeSpecification section = hypipeStraight.Section;
			foreach (Connector connector in RevitUtils.GetElementConnectors(rvtFi))
			{
				if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
				{
					RevitPipeSystemUpdate.ToConnector(section, connector);
				}
			}
			return true;
		}

		private static bool ToFitting(HYPipeFitting hyFitting, FamilyInstance rvtFi)
		{
			MechanicalFitting mechanicalFitting = rvtFi.MEPModel as MechanicalFitting;
			if (mechanicalFitting == null)
			{
				return false;
			}
            switch ((int)mechanicalFitting.PartType)
			{
			case 5:
				RevitPipeSystemUpdate.ToElbow(hyFitting as HYPipeElbow, rvtFi);
				break;
			case 6:
				RevitPipeSystemUpdate.ToTee(hyFitting as HYPipeTee, rvtFi);
				break;
			case 7:
				RevitPipeSystemUpdate.ToTrans(hyFitting as HYPipeTransition, rvtFi);
				break;
			case 8:
				RevitPipeSystemUpdate.ToCross(hyFitting as HYPipeCross, rvtFi);
				break;
			}
			return true;
		}

		private static bool ToTrans(HYPipeTransition hyTrans, FamilyInstance rvtFi)
		{
			HYPipeSpecification hypipeSpecification = hyTrans.InPort as HYPipeSpecification;
			HYPipeStraight hypipeStraight = hyTrans.GetPreviousPipeline() as HYPipeStraight;
			if (hypipeStraight != null)
			{
				RevitPipeSystemUpdate.ToConnector(hypipeStraight.Section, hypipeSpecification.Tag as Connector);
			}
			HYPipeSpecification hypipeSpecification2 = hyTrans.OutPort as HYPipeSpecification;
			HYPipeStraight hypipeStraight2 = hyTrans.GetHostPipeline() as HYPipeStraight;
			if (hypipeStraight2 != null)
			{
				RevitPipeSystemUpdate.ToConnector(hypipeStraight2.Section, hypipeSpecification2.Tag as Connector);
			}
			return true;
		}

		private static bool ToElbow(HYPipeElbow hyElbow, FamilyInstance rvtFi)
		{
			HYPipeStraight hypipeStraight = hyElbow.GetHostPipeline() as HYPipeStraight;
			if (hypipeStraight == null)
			{
				return false;
			}
			HYPipeSpecification section = hypipeStraight.Section;
			foreach (Connector conn in RevitUtils.GetElementConnectors(rvtFi))
			{
				RevitPipeSystemUpdate.ToConnector(section, conn);
			}
			return true;
		}

		private static bool ToTee(HYPipeTee hyTee, FamilyInstance rvtFi)
		{
			HYPipeSpecification hypipeSpecification = hyTee.InPort as HYPipeSpecification;
			HYPipeStraight hypipeStraight = hyTee.GetPreviousPipeline() as HYPipeStraight;
			HYPipeSpecification section = hypipeStraight.Section;
			if (hypipeStraight != null)
			{
				RevitPipeSystemUpdate.ToConnector(section, hypipeSpecification.Tag as Connector);
			}
			HYPipeSpecification hypipeSpecification2 = hyTee.OutPort as HYPipeSpecification;
			HYPipeStraight hypipeStraight2 = hyTee.GetHostPipeline() as HYPipeStraight;
			if (hypipeStraight2 != null)
			{
				RevitPipeSystemUpdate.ToConnector(section, hypipeSpecification2.Tag as Connector);
			}
			HYPipeSpecification sidePort = hyTee.SidePort;
			HYPipeStraight hypipeStraight3 = hyTee.GetSideHostPipeline() as HYPipeStraight;
			if (hypipeStraight3 != null)
			{
				RevitPipeSystemUpdate.ToConnector(section, sidePort.Tag as Connector);
			}
			return true;
		}

		private static bool SetReduce(Connector connector)
		{
			Connector connector2 = null;
			if (connector.IsConnected)
			{
				connector2 = YJKRevitTools.GetConnectConnector(connector);
			}
			if (connector2 == null)
			{
				return false;
			}
			Element owner = connector2.Owner;
			if (owner == null)
			{
				return false;
			}
			FamilyInstance familyInstance = owner as FamilyInstance;
			if (familyInstance == null)
			{
				return false;
			}
			MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
			if (mechanicalFitting == null)
			{
				return false;
			}
			if ((int)mechanicalFitting.PartType != 7)
			{
				return false;
			}
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(familyInstance);
			Connector connector3 = (elementAllConnector[0].Radius > elementAllConnector[1].Radius) ? elementAllConnector[0] : elementAllConnector[1];
			Parameter parameter = familyInstance.GetParameter("偏移高度");
			if (parameter != null)
			{
				parameter.Set(connector3.Radius);
			}
			parameter = familyInstance.GetParameter("偏移宽度");
			if (parameter != null)
			{
				parameter.Set(connector3.Radius);
			}
			parameter = familyInstance.GetParameter("阀体厚度");
			if (parameter != null)
			{
				parameter.Set(connector3.Radius * 2.0);
			}
			return true;
		}

		private static bool ToCross(HYPipeCross hyCross, FamilyInstance rvtFi)
		{
			RevitPipeSystemUpdate.ToTee(hyCross, rvtFi);
			HYPipeStraight hypipeStraight = hyCross.GetPreviousPipeline() as HYPipeStraight;
			HYPipeSpecification side2Port = hyCross.Side2Port;
			HYPipeStraight hypipeStraight2 = hyCross.GetSide2HostPipeline() as HYPipeStraight;
			if (hypipeStraight2 != null)
			{
				RevitPipeSystemUpdate.ToConnector(hypipeStraight.Section, side2Port.Tag as Connector);
			}
			return true;
		}

		private HYPipeSystemDocument m_Document;

		private Document m_RevitDocument;

		private List<Pipe> m_RevitPipes;

		protected string lastSelectedPath = string.Empty;
	}
}
